package com.kinon.norogamilk.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kinon.norogamilk.R;
import com.kinon.norogamilk.adapter.RecyclRaodAdapter;
import com.kinon.norogamilk.bean.BaseBean;
import com.kinon.norogamilk.bean.RepairBean;
import com.kinon.norogamilk.bean.SpecBean;
import com.kinon.norogamilk.http.CommonSubscriber;
import com.kinon.norogamilk.http.MilkApi;
import com.kinon.norogamilk.http.StrCallback;
import com.kinon.norogamilk.ui.base.BaseActivity;
import com.kinon.norogamilk.util.RxUtil;

import java.util.ArrayList;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2017-12-20.
 * 新补货
 */

public class NewRepairActivity extends BaseActivity implements
        View.OnClickListener, RecyclRaodAdapter.OnMyItemClickListener {

    protected CompositeDisposable mCompositeDisposable;
    private Button btn_back;
    private Button btn_sumbit;
    private TextView tv_name1, tv_name2, tv_name3, tv_name4;
    private EditText et_begin1, et_begin2, et_begin3, et_begin4;
    private EditText et_end1, et_end2, et_end3, et_end4;
    private EditText et_num1, et_num2, et_num3, et_num4;
    private Button btn_determine;
    private ArrayList<RepairBean> repairList;
    private ArrayList<SpecBean> goodList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newrepair_layout);
        initView();

    }

    private void initView() {
        repairList = new ArrayList<>();
        btn_back = findViewById(R.id.btn_back);
        btn_sumbit = findViewById(R.id.btn_sumbit);
        tv_name1 = findViewById(R.id.tv_name1);
        tv_name2 = findViewById(R.id.tv_name2);
        tv_name3 = findViewById(R.id.tv_name3);
        tv_name4 = findViewById(R.id.tv_name4);
        et_begin1 = findViewById(R.id.et_begin1);
        et_begin2 = findViewById(R.id.et_begin2);
        et_begin3 = findViewById(R.id.et_begin3);
        et_begin4 = findViewById(R.id.et_begin4);
        et_end1 = findViewById(R.id.et_end1);
        et_end2 = findViewById(R.id.et_end2);
        et_end3 = findViewById(R.id.et_end3);
        et_end4 = findViewById(R.id.et_end4);
        et_num1 = findViewById(R.id.et_num1);
        et_num2 = findViewById(R.id.et_num2);
        et_num3 = findViewById(R.id.et_num3);
        et_num4 = findViewById(R.id.et_num4);
        btn_determine = findViewById(R.id.btn_determine);
        goodList = new ArrayList<>();
        getGoods();
        addListener();
    }

    private void listAdd() {
        repairList.clear();
        if (goodList.size() <= 0) {
            mToast("加入货道失败");
            return;
        }
//        tv_name1.setText(goodList.get(0).getSpecs().get(1).getSpec());
//        tv_name2.setText(goodList.get(0).getSpecs().get(0).getSpec());
//        tv_name3.setText(goodList.get(1).getSpecs().get(1).getSpec());
//        tv_name4.setText(goodList.get(1).getSpecs().get(0).getSpec());
        isDataNull(et_begin1, et_end1, et_num1, goodList.get(0).getSpecs().get(1).getGoods_id());
        isDataNull(et_begin2, et_end2, et_num2, goodList.get(0).getSpecs().get(0).getGoods_id());
        isDataNull(et_begin3, et_end3, et_num3, goodList.get(1).getSpecs().get(1).getGoods_id());
        isDataNull(et_begin4, et_end4, et_num4, goodList.get(1).getSpecs().get(0).getGoods_id());


    }

    private void isDataNull(EditText et, EditText et2, EditText et3, int id) {
        String start = et.getText().toString();
        String end = et2.getText().toString();
        String num = et3.getText().toString();
        if (!TextUtils.isEmpty(start) && !TextUtils.isEmpty(end)
                && !TextUtils.isEmpty(num)) {
            repairList.add(new RepairBean(start.toUpperCase(),
                    end.toUpperCase(), num, id));

        }

    }


    private void addListener() {
        btn_back.setOnClickListener(this);
        btn_sumbit.setOnClickListener(this);
        btn_determine.setOnClickListener(this);
    }

    private String getRepairData() {
//        repList.add(new RepairBean(3,mMaxNum));
//        repList.add(new RepairBean(4,mMinNum));
        Gson gson = new Gson();
        String data = gson.toJson(repairList);
        Log.e("lh", "getRepairData==" + data);
        return data;
    }


    private void restock() {
        btn_sumbit.setEnabled(false);
        Call<String> call = MilkApi.getInstance()
                .machinePw(sn, getRepairData());
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                mToast("补货成功");
                btn_sumbit.setEnabled(true);
                finish();
            }

            @Override
            public void onError(int code, String error) {
                btn_sumbit.setEnabled(true);
                mToast(error);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:

                finish();
                break;
            case R.id.btn_determine:

                listAdd();
                mToast("加入货道成功");
                break;
            case R.id.btn_sumbit:

                restock();
                break;

        }
    }

    private void getGoods() {
        mCompositeDisposable = new CompositeDisposable();
        Disposable subscription = MilkApi.getInstance()
                .getGoods(sn)
                .compose(RxUtil.<BaseBean<ArrayList<SpecBean>>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<BaseBean<ArrayList<SpecBean>>>() {
                    @Override
                    public void onNext(BaseBean<ArrayList<SpecBean>> baseBean) {
//                        baseBean.getData();
//                        setView(baseBean);
                        if (baseBean.getData() != null && baseBean.getData().size() > 0) {
                            goodList.addAll(baseBean.getData());
                            tv_name1.setText("酸奶" + goodList.get(0).getSpecs().get(1).getSpec());
                            tv_name2.setText("酸奶" + goodList.get(0).getSpecs().get(0).getSpec());
                            tv_name3.setText("鲜奶" + goodList.get(1).getSpecs().get(1).getSpec());
                            tv_name4.setText("鲜奶" + goodList.get(1).getSpecs().get(0).getSpec());
                        }

                    }

                    @Override
                    public void onFail(Throwable t, int code, String msg) {

                    }
                });
        mCompositeDisposable = new CompositeDisposable();
        mCompositeDisposable.add(subscription);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCompositeDisposable.clear();
    }


    @Override
    public void onItemClick(View view, int position) {
//        Log.e(TAG, "position =="+position);
//        raodIndex=raodList.get(position).getGoods_id();
    }
}
